package t71

import (
	"errors"
	"strings"
	"sync"
)

//{"t1", args{path: "/a/../../b/../c//.//"}, "/c"},
//{"t1", args{path: "/a//b////c/d//././/.."}, "/a/b/c"},
func simplifyPath(path string) string {

	stack := MyStack{}

	s := strings.Split(path, "/")

	for _, v := range s {
		//当前目录
		if v == "." || v == "" {
			continue
		}
		//上级目录
		if v == ".." {
			_, _ = stack.Pop()
			continue
		}
		stack.Push(v)
	}
	return "/" + stack.String("/")
}

type MyStack struct {
	items []string
	l     sync.Mutex
}

func (m *MyStack) Push(n string) {
	m.l.Lock()
	defer m.l.Unlock()
	m.items = append(m.items, n)
}

func (m *MyStack) Pop() (string, error) {
	m.l.Lock()
	defer m.l.Unlock()
	if len(m.items) == 0 {
		return "", errors.New("empty of stack")
	}
	el := m.items[len(m.items)-1]
	m.items = m.items[:len(m.items)-1]
	return el, nil
}

func (m *MyStack) Top() (string, error) {
	m.l.Lock()
	defer m.l.Unlock()
	if len(m.items) == 0 {
		return "", errors.New("empty of stack")
	}
	el := m.items[len(m.items)-1]
	return el, nil
}

func (m *MyStack) Size() int {
	return len(m.items)
}

func (m *MyStack) Empty() bool {
	if len(m.items) == 0 {
		return true
	}
	return false
}

func (m *MyStack) Clear() {
	m.items = m.items[0:0]
}

func (m *MyStack) String(sep string) string {
	return strings.Join(m.items, sep)
}
